<template>
  <div style="background: #0d5593;height: 900px">
    <ul v-for="(rowContent,rowIndex) in rowContents" :key="rowIndex" class="menu">
      <!--这里写的 'bg-img-'+(pIndex+1) 与style的相关值对应-->
      <li v-for="(polygon,pIndex) in rowContent.polygons" :key="pIndex" :class="['menu-cell','bg-img-'+(pIndex+1)]" @click="onPolygonClick(polygon.eventName)">
        <img v-if="polygon.imgUrl" class="menu-cell__image" :src="polygon.imgUrl">
        <div v-if="polygon.text" class="menu-cell__title">{{ polygon.text }}</div>
      </li>
      <li class="menu-cell menu__placeholder" />
    </ul>
  </div>
</template>

<script>
export default {
  name: 'HoneycombImg',
  props: {
    rowContents: { type: Array, default: () => {
      return [
        { polygons: [
          { imgUrl: require('../assets/logo.png'), text: '111', eventName: '' },
          { imgUrl: require('../assets/logo.png'), text: '111', eventName: '' },
          { imgUrl: require('../assets/logo.png'), text: '111', eventName: '' },
          { imgUrl: require('../assets/logo.png'), text: '111', eventName: '' }
        ] },
        { polygons: [
          { imgUrl: require('../assets/logo.png'), text: '111', eventName: '' },
          { imgUrl: require('../assets/logo.png'), text: '111', eventName: '' },
          { imgUrl: require('../assets/logo.png'), text: '111', eventName: '' }
        ] },
        { polygons: [
          { imgUrl: require('../assets/logo.png'), text: '111', eventName: '' },
          { imgUrl: require('../assets/logo.png'), text: '111', eventName: '' },
          { imgUrl: require('../assets/logo.png'), text: '111', eventName: '' },
          { imgUrl: require('../assets/logo.png'), text: '111', eventName: '' }
        ] }
      ]
    } } // 每一行(包含多边形)要显示的内容(相邻的两行的多边形数量不能相等并且只能相差一个，否则UI错乱) todo 待优化
  },
  data() {
    return {
    }
  },
  watch: {
  },
  mounted() {
  },
  created() {
  },
  methods: {
    onPolygonClick(eventName) {
      console.log('polygon trigger eventName is :', eventName)
      this.$emit('eventName')
    }
  }
}
</script>

<style lang="scss" scoped>
  /*多边形展示的背景图片*/
  $bg-img-arr:
  (name:'test01',hoverName:'test02',suffix:'.jpg',hoverSuffix:'.jpg'),
  (name:'test02',hoverName:'test03',suffix:'.jpg',hoverSuffix:'.jpg'),
  (name:'test03',hoverName:'test02',suffix:'.jpg',hoverSuffix:'.jpg');
  /*背景图存放的文件夹*/
  $bg-img-folder: '../static/img/';

  *, *:before, *:after {
    box-sizing: border-box;
  }
  .menu {
    display: flex;
    flex-wrap: wrap; // flex 超出宽度，则折行显示 配合min-width 使其不折行，避免意外
    list-style: none;
    justify-content: center;
    align-items: center;
    min-width: 550px;
    margin: 0 auto;
    padding: 0;
    -webkit-transform: translateY(34.375px);
    transform: translateY(34.375px);
  }
  .menu-cell {
    flex: 0 1 100px; // 间接控制多边形大小-宽度
    min-width: 100px;
    height: 60px; // 间接控制多边形大小-高度
    margin: 20px 4px 16px; // 控制多边形之间的间距
    position: relative;
    padding: 0.5em;
    text-align: center;
    background: #0a7993;
    z-index: 1;
  }
  // 兼容小设备，适应浏览器缩放（根据情况调整如下参数）
  @media screen and (max-width:960px){
    .menu-cell {
      flex: 0 1 70px; // 间接控制多边形大小-宽度
      min-width: 70px;
      height: 40px; // 间接控制多边形大小-高度
      margin: 13px 2.5px 12px; // 控制多边形之间的间距
    }
    .menu {
      min-width: 350px;
    }
  }
  @media screen and (min-width:960px) and (max-width:1200px){
    .menu-cell {
      flex: 0 1 85px; // 间接控制多边形大小-宽度
      min-width: 85px;
      height: 50px; // 间接控制多边形大小-高度
      margin: 16px 3px 14px; // 控制多边形之间的间距
    }
    .menu {
      min-width: 450px;
    }
  }
  .menu__placeholder {
    display: none;
    opacity: 0;
    width: 250px;
    margin: 0 12.5px;
  }
  .menu-cell__image {
    -o-object-fit: cover;
    object-fit: cover;
    -o-object-position: center;
    object-position: center;
    border-style: none;
  }
  .menu-cell__image{
    width: 30%;
    height: 30%;
    margin-top: -10px;
  }
  .menu-cell__title {
    height: 40px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    -webkit-hyphens: auto;
    -ms-hyphens: auto;
    hyphens: auto;
    word-break: break-word;
    text-transform: uppercase;
    color: #fff;
    font-weight: 700;
    font-size: 1.35em;
    transition: opacity 350ms;
  }

  /*没有多边形背景图的样式 start*/
  .menu-cell {
    &:before {
      background: #0a7993;
      -webkit-transform: scale(1.055);
      transform: scale(1.055);
      content: '';
    }
    &:hover:before{
      background: #ffb741;
      -webkit-transform: scale(1.055);
      transform: scale(1.055);
    }
    &:after {
      background: #3ea0eb;
      opacity: 0.5;
      transition: opacity 350ms;
      content: '';
    }
    &:hover:after{
      opacity: 0.2;
      transition: opacity 350ms;
    }
  }
  .menu-cell:before, .menu-cell:after {
    top: -50%;
    left: 0;
    width: 100%;
    height: 200%;
    display: block;
    position: absolute;
    -webkit-clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
    clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
    z-index: -1;
  }
  /*没有多边形背景图的样式 end*/
  /*有多边形背景图(动态循环出来的背景图)的样式 start*/
  @for $i from 1 through length($bg-img-arr) {
    $item: nth($bg-img-arr, $i);
    .menu-cell.bg-img-#{$i}{
      &:before, &:after {
        background: url($bg-img-folder + map-get($item,name) + map-get($item,suffix)) no-repeat;
        background-size: 100% 100%;
      }
      &:hover:before, &:hover:after {
        background: url($bg-img-folder + map-get($item,hoverName) + map-get($item,hoverSuffix)) no-repeat;
        background-size: 100% 100%;
      }
    }
  }
  /*有多边形背景图(动态循环出来的背景图)的样式 end*/
</style>
